package org.yx.service.sbas.save_workshop;

import org.yx.exception.BizException;
import org.yx.models.SbasWspDef;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;

import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 保存车间信息
 * @author zxw
 * @since 2018/04/14
**/
public class SbasSaveWorkshopService extends BaseService {
	public SbasSaveWorkshopOut saveWorkshop(SbasSaveWorkshopIn in) throws Exception {
		if (StrUtil.isBlank(in.getShopCode())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "shopCode"));
		}

		SbasWspDef oldShop = selectWithPk(SbasWspDef.class, in.getUserOrgCode(), in.getShopCode());
		SbasWspDef shop = oldShop == null ? new SbasWspDef() : oldShop;
		copyProperties(in, shop);
		if (new Character('D').equals(shop.getStatus())) {
			throw new BizException("SBAS-0004", new FieldMap("shopCode", shop.getShopCode()));
		}

		if (new Character('D').equals(in.getStatus())) {
			if (oldShop == null) {
				throw new BizException("SBAS-0004", new FieldMap("shopCode", shop.getShopCode()));
			}
			shop.setStatus(in.getStatus());

			update(shop, "status");
		} else {
			shop.setStatus('N');
			
			upsert(shop);
		}

		SbasSaveWorkshopOut out = new SbasSaveWorkshopOut();

		return out;
	}
}